package com.tracy.lifeproject.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.bean.WebBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.utils.OKHttpUtils;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class DetailActivity extends AppCompatActivity {

    @InjectView(R.id.img_detail_back)
    ImageView imgDetailBack;
    @InjectView(R.id.toolbar_detail)
    Toolbar toolbarDetail;
    @InjectView(R.id.img_detail_good)
    ImageView imgDetailGood;
    @InjectView(R.id.text_detail_good)
    TextView textDetailGood;
    @InjectView(R.id.img_detail_shere)
    ImageView imgDetailShere;
    @InjectView(R.id.text_detail_share)
    TextView textDetailShare;
    @InjectView(R.id.img_detail_comment)
    ImageView imgDetailComment;
    @InjectView(R.id.text_detail_comment)
    TextView textDetailComment;
    @InjectView(R.id.webView_detail)
    WebView webViewDetail;
    @InjectView(R.id.btn_detail_back)
    TextView btnDetailBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.inject(this);
        //获取数据
        initData();


    }

    /**
     * 获取数据
     */
    private void initData() {
        /**
         * 接受跳转数据
         */
        Intent intent = getIntent();
        int URL_DETAIL = intent.getIntExtra("id", 0);

        OKHttpUtils.newInstance(this).getAsyncMainData(UrlString.URL_WEB + URL_DETAIL, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Toast.makeText(DetailActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String result) {
                WebBean bean = new Gson().fromJson(result, WebBean.class);

                initView(bean);
            }
        }, 123);
    }

    /**
     * 设置内容
     */
    private void initView(WebBean bean) {
        WebSettings webSettings = webViewDetail.getSettings();
        webSettings.setDisplayZoomControls(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webViewDetail.loadDataWithBaseURL(null, bean.getData().getContent_html(), "text/html", "utf-8", null);
        textDetailGood.setText(Integer.toString(bean.getData().getLikes_count()));
        textDetailShare.setText(Integer.toString(bean.getData().getShares_count()));
        textDetailComment.setText(Integer.toString(bean.getData().getStatus()));
    }


    /**
     * 点击事件
     */
    @OnClick({R.id.img_detail_back, R.id.btn_detail_back, R.id.img_detail_good, R.id.img_detail_shere, R.id.img_detail_comment})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_detail_back:
                finish();
                break;
            case R.id.btn_detail_back:
                finish();
                break;
            case R.id.img_detail_good:
                break;
            case R.id.img_detail_shere:
                break;
            case R.id.img_detail_comment:
                break;
        }
    }
}
